package service;
import exception.BookBorrowedException;
import exception.BorrowLimitExceededException;
import model.Book;
import model.Reader;
import model.BorrowRecord;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class Library {
    private List<Book> bookList;
    private List<Reader> readerList;
    private List<BorrowRecord> recordList;
    public Library() {
        bookList = new ArrayList<>();
        readerList = new ArrayList<>();
        recordList = new ArrayList<>();
        bookList.add(new Book("B001", "Java编程思想", "Bruce Eckel"));
        readerList.add(new Reader("R001", "张三"));
    }
    public void borrowBook(String bookIsbn, String readerId) {
        try {
            Book book = findBookByIsbn(bookIsbn);
            if (book == null) {
                System.out.println("错误：图书不存在！");
                return;
            }
            Reader reader = findReaderById(readerId);
            if (reader == null) {
                System.out.println("错误：读者不存在！");
                return;
            }
            if (book.isBorrowed()) {
                throw new BookBorrowedException("错误：图书" + bookIsbn + "已借出！");//////
            }
            if (!reader.canBorrow()) {
                throw new BorrowLimitExceededException("错误：读者" + readerId + "已借满3本！");
            }
            book.setBorrowed(true);
            reader.addBorrowCount();
            BorrowRecord record = new BorrowRecord(book, reader);
            recordList.add(record);
            saveBorrowRecord(record);//
            System.out.println("借书成功！" + book + " | " + reader);
        } catch (BookBorrowedException | BorrowLimitExceededException e) {
            System.out.println(e.getMessage());
        }
    }
    public void returnBook(String bookIsbn) {
        Book book = findBookByIsbn(bookIsbn);
        if (book == null || !book.isBorrowed()) {
            System.out.println("错误：图书未借出或不存在！");
            return;
        }
        for (BorrowRecord record : recordList) {
            if (record.toString().contains(bookIsbn) && record.toString().contains("未还书")) {
                book.setBorrowed(false);
                record.getReader().reduceBorrowCount();
                record.setReturnTime();
                saveBorrowRecord(record);////////
                System.out.println("还书成功！" + book);
                return;
            }
        }
        System.out.println("错误：未找到该图书的借阅记录！");
    }
    public void showAllRecords() {
        if (recordList.isEmpty()) {
            System.out.println("暂无借阅记录！");
            return;
        }
        System.out.println("=== 借阅记录 ===");
        for (BorrowRecord record : recordList) {
            System.out.println(record);
        }
    }
    private Book findBookByIsbn(String isbn) {
        for (Book book : bookList) {
            if (book.getIsbn().equals(isbn)) {
                return book;
            }
        }
        return null;
    }
    private Reader findReaderById(String readerId) {
        for (Reader reader : readerList) {
            if (reader.getReaderId().equals(readerId)) {
                return reader;
            }
        }
        return null;
    }
    private void saveBorrowRecord(BorrowRecord record) {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter("borrow_records.txt", true))) {
            writer.write(record.toString());
            writer.newLine();
        } catch (IOException e) {
            System.out.println("记录保存失败：" + e.getMessage());
        }
    }
}
